﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.TBKN;
using BLL.TBKN;
using Entities.Admin;
 

namespace WindowsForms.TBKN
{
    public partial class FormThemKhieuNai : Form
    {
        public static string maNhanVien;
        int trangThai;
        public FormThemKhieuNai()
        {
            InitializeComponent();
            
            
           // btnUpdate.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Bạn có muốn thoát khỏi chương trình ?", "Question",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                this.Close();
            }
            else
            {
                Application.Restart();
            }
        }

        private void cbbTrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbTrangThai.SelectedIndex > 0)
            {
                trangThai = cbbTrangThai.SelectedIndex;
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenKhieuNai.Text.Trim() != "" && txtNguoiGui.Text.Trim() != "" &&
                    cbbTrangThai.SelectedIndex > 0 && cbbNguoiNhan.SelectedIndex > 0
                    && QuanLyKhieuNai.KiemTraMaKhieuNai(mtxtMaKhieuNai.Text))
                {
                    KhieuNai kn = new KhieuNai();
                    kn.MaKhieuNai = mtxtMaKhieuNai.Text;
                    kn.TenKhieuNai = txtTenKhieuNai.Text;
                    kn.NgayGui = DateTime.Parse(dtpNgayGui.Text);
                    kn.NgayTraLoi = DateTime.Parse(dtpNgayTraLoi.Text);
                    kn.NguoiGui = txtNguoiGui.Text;
                    kn.IdNguoiNhan = maNhanVien;
                    kn.NoiDung = txtNoiDung.Text;
                    kn.TrangThai = trangThai;
                    if (QuanLyKhieuNai.KiemTraMaKhieuNai(mtxtMaKhieuNai.Text))
                    {
                        if (QuanLyKhieuNai.KiemTra(mtxtMaKhieuNai.Text,
                            KhieuNai.Objects.All().ToList()))
                        {
                            bool kq = KhieuNai.Objects.Add(kn);
                            if (kq)
                            {
                                MessageBox.Show("Thêm thành công");
                            }
                        }
                        else
                        {
                            DialogResult rs = MessageBox.Show("Khiếu nại có mã " + mtxtMaKhieuNai.Text + " đã tồn tại bạn có muốn cập nhât không? ", "Hỏi", MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question);
                            if (rs == DialogResult.Yes)
                            {
                                KhieuNai kn1 = KhieuNai.Objects.GetById(kn.MaKhieuNai);
                                KhieuNai.Objects.Update(kn);
                            }
                        }
                    }
                }
                else
                {
                    DialogResult rs= MessageBox.Show("chưa nhập đầy đủ dữ liệu, vui lòng nhập lại","cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (rs == DialogResult.OK)
                    {
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception)
            {

                
            }        
        }

        private void LoadComboBox(List<NhanVien> list)
        {
            foreach (var item in list)
                cbbNguoiNhan.Items.Add(item);
            cbbNguoiNhan.DisplayMember = "TenNhanVien";
            cbbNguoiNhan.ValueMember = "MaNhanVien";
            cbbNguoiNhan.Items.Insert(0, "---Nhân viên---");
            cbbNguoiNhan.SelectedIndex = 0;
        }

        private void mtxtMaKhieuNai_Validating(object sender, CancelEventArgs e)
        {
            if (!QuanLyKhieuNai.KiemTraMaKhieuNai(mtxtMaKhieuNai.Text))
            {
                MaKNerrorProvider1.SetError(mtxtMaKhieuNai, "mã khiếu nại phải có 7 ký tự dạng (KN00000)");
                e.Cancel = true;
            }
        }

        private void mtxtMaKhieuNai_Validated(object sender, EventArgs e)
        {
            MaKNerrorProvider1.SetError(mtxtMaKhieuNai, null);
        }

        private void FormThemKhieuNai_Load(object sender, EventArgs e)
        {
            cbbTrangThai.SelectedIndex = 0;
            
            LoadComboBox(NhanVien.Objects.Find(x => x.MaBoPhan == "KN_001").ToList());
            cbbNguoiNhan.SelectedIndex = 0;
        }

        private void cbbNguoiNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbbNguoiNhan.SelectedIndex>0)
            {
                maNhanVien = (cbbNguoiNhan.SelectedItem as NhanVien).MaNhanVien;
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            this.mtxtMaKhieuNai.Text = "";
            this.txtNguoiGui.Text = "";
            this.txtNoiDung.Text = "";
            this.txtTenKhieuNai.Text = "";
            this.cbbNguoiNhan.SelectedIndex = 0;
        }
    }
}
